package com.yaj.myfactorysupport.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yaj.myfactorysupport.dto.TLightInfoAccompDto;
import com.yaj.myfactorysupport.entity.TLightInfoAccomp;
import com.yaj.myfactorysupport.mapper.TLightInfoAccompMapper;
import com.yaj.myfactorysupport.service.TLightInfoAccompService;
import com.yaj.myfactorysupport.util.ErrorCodeEnum;
import com.yaj.myfactorysupport.util.RespBean;
import com.yaj.myfactorysupport.util.SucessCodeEnum;
import com.yaj.myfactorysupport.vo.TLightInfoAccompVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wei
 * @since 2022-01-03
 */
@Slf4j
@Service
public class TLightInfoAccompServiceImpl extends ServiceImpl<TLightInfoAccompMapper, TLightInfoAccomp> implements TLightInfoAccompService {
    @Resource
    private TLightInfoAccompMapper tLightInfoAccompMapper;

    @Override
    public RespBean RegTLightInfoAccomp(TLightInfoAccompDto tLightInfoAccompDto) {
        try {
            TLightInfoAccomp tLightInfoAccomp = new TLightInfoAccomp();
            BeanUtil.copyProperties(tLightInfoAccompDto,tLightInfoAccomp);
            tLightInfoAccomp.setOccurTimestamp(DateUtil.date());
            int result = tLightInfoAccompMapper.insert(tLightInfoAccomp);
            if(result==1){
                return RespBean.success(SucessCodeEnum.报告故障成功.getMsg());
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        return RespBean.error(ErrorCodeEnum.插入记录失败.getCode(),ErrorCodeEnum.插入记录失败.getMsg());
    }

    @Override
    public RespBean getTLightInfoAccompList() {
        List<TLightInfoAccompVo> tLightInfoAccompList = tLightInfoAccompMapper.getTLightInfoAccompList();
        return RespBean.success(tLightInfoAccompList);
    }

    @Override
    public RespBean recoveryTLightInfoAccomp(BigDecimal uid,String user) {
        Map<String,Object> parameterMap = new HashMap<String,Object>();
        parameterMap.put("uid",uid);
        parameterMap.put("user",user);
        int result = tLightInfoAccompMapper.recoveryTLightInfoAccomp(parameterMap);
        if(result ==1){
            return RespBean.success(SucessCodeEnum.恢复故障成功);
        }
        return RespBean.error(ErrorCodeEnum.更新记录失败.getCode(),ErrorCodeEnum.更新记录失败.getMsg());
    }

    @Override
    public RespBean repairTLightInfoAccomp(BigDecimal uid, String user) {
        Map<String,Object> parameterMap = new HashMap<String,Object>();
        parameterMap.put("uid",uid);
        parameterMap.put("user",user);
        int result = tLightInfoAccompMapper.repairTLightInfoAccomp(parameterMap);
        if(result ==1){
            return RespBean.success(SucessCodeEnum.报告修理成功);
        }
        return RespBean.error(ErrorCodeEnum.更新记录失败.getCode(),ErrorCodeEnum.更新记录失败.getMsg());
    }
}
